define([],function(){
	
	//head img area
	var headImg 		= $('#centerImg');

	//control panel
	var onPlay_btn 			= $('#playstop');
	var onNext_btn 			= $('#next');
	var onPrev_btn			= $('#prev');
	var onVolumn_btn		= $('#volume');
	var onMenu_btn			= $('#muiscmenu');
	var onMenu_btn_close	= $('.close');
	var muisc  				= document.getElementById("music");
	var musicBox			= $('.music');
	var durationProgress	= $('#durationProgress');
	var setIn ; 
	var timeLine 			= $('.timeLine');
	var durationArea 		= $('.durationArea');
	var music_select		= $('#music_select');
	var loadingArea			= $('.loadingArea');
	var loadMusicSetIn;

	var playList 			= [
		{
			'id'   :'1',
			'title':'有何不可',
			'album':'有何不可-许嵩',
			'url'  :'http://jscore.secretcastle.cn/file/m1.mp3',
			'img'  :'http://jscore.secretcastle.cn/img/pic.jpg'
		},
		{
			'id'   :'2',
			'title':'Brave Heart',
			'album':'Brave Heart',
			'url'  :'http://jscore.secretcastle.cn/file/m2.mp3',
			'img'  :'http://jscore.secretcastle.cn/img/pic2.jpg'
		},
		{
			'id'   :'3',
			'title':'Summer',
			'album':'Summer',
			'url'  :'http://jscore.secretcastle.cn/file/m3.mp3',
			'img'  :'http://jscore.secretcastle.cn/img/pic3.jpg'
		}
	];

	return {
		init:function(){
			var self = this;
			self.loadFileInMusicList();
			self.eventAction();
			self.initLoad();
		},
		onPlay:function(){
			var self = this;
			onPlay_btn.removeClass("icon-yulebofang").addClass("icon-yulezanting");
			// headImg.removeClass('rotateImg').addClass('rotateImg');
			// music.play();
			loadMusicSetIn = setInterval(self.loadMusic,1000);
			setIn = setInterval(self.updateProress,500);

		},
		onPause:function(){
			onPlay_btn.removeClass("icon-yulezanting").addClass("icon-yulebofang");
			headImg.removeClass('rotateImg');
			music.pause();
			clearInterval(setIn);
		},
		onVolumn:function(){
			if(music.muted == false){
				music.muted = true;
				onVolumn_btn.removeClass("icon-23kechengbofangshengyin").addClass("icon-volume-muted");	
			}else{
				music.muted = false;
				onVolumn_btn.removeClass("icon-volume-muted").addClass("icon-23kechengbofangshengyin");	
			}
		},

		//next music & prev music
		onNext:function(){
			var self = this;
			var musicIndex;
			var cur = musicBox.find('source').attr('src');
			//console.log(cur);
			for(var i = 0 ; i < playList.length ; i++){
				if(cur == playList[i].url){
					musicIndex = i;
					break;
				}
			}
			self.playIndex(musicIndex+1);
		},
		onPrev:function(){
			var self = this;
			var musicIndex;
			var cur = musicBox.find('source').attr('src');
			//console.log(cur);
			for(var i = 0 ; i < playList.length ; i++){
				if(cur == playList[i].url){
					musicIndex = i;
					break;
				}
			}
			self.playIndex(musicIndex-1);
		},
		// show&hide music menu
		onMenu:function(){
			var musicMenu  = $('#music_list')
			var hasAnimate = musicMenu.hasClass('show-music-list');
			if(!hasAnimate){
				musicMenu.removeClass('hide-music-list').addClass('show-music-list');
			}
		},
		onMenuClose:function(){
			var musicMenu  = $('#music_list')
			var hasAnimate = musicMenu.hasClass('show-music-list');
			if(hasAnimate){
				musicMenu.removeClass('show-music-list').addClass('hide-music-list');
			}
		},
		// slide progress
		onduration:function(){

		},
		//events group
		eventAction:function(){
			var self = this;
			//play event
			onPlay_btn.on('click',function(){
				var hasThis = onPlay_btn.hasClass("icon-yulebofang");
				if(hasThis){
					self.onPlay();
				}else{
					self.onPause();
				}
			});
			//volumn set
			onVolumn_btn.on('click',function(){
				self.onVolumn();
			});
			//music_menu set
			onMenu_btn.on('click',function(){
				self.onMenu();
			});
			onMenu_btn_close.on('click',function(){
				self.onMenuClose();
			});
			//next & prev set
			onNext_btn.on('click',function(){
				self.onNext();
			});
			onPrev_btn.on('click',function(){
				self.onPrev();
			});

			//hide area panel 
			$('#music_list li').on('click',function(){
				var selected = $(this).attr('id');
				if(selected){
					$("#music_list").removeClass('show-music-list').addClass('hide-music-list');
					self.playIndex(selected);
					self.loadTopPic(selected);
				}
			});
		},

		//update progress
		updateProress:function(){
			var cur = music.currentTime;
			var timeSet = parseInt(cur%60) < 10 ? "0" + parseInt(cur%60) : parseInt(cur%60);
				minSet  = parseInt(cur/60) < 10 ? "0" + parseInt(cur/60) : parseInt(cur/60);
				perSet 	= (cur/music.duration) * 100;
			timeLine.text(minSet +":"+timeSet);
			durationProgress.attr('value', parseInt(perSet));
		},

		//check group
		hasEnded:function(){
			console.log('run');
		},

		//utils -> useless in this application
		checkSupportACO:function(){
			var xmlHttp ;
			if(window.ActiveXObject){
				xmlHttp = new ActiveXObject("Scripting.FileSystemObject");
			}else if(window.XMLHttpRequest){
				xmlHttp = new XMLHttpRequest("Scripting.FileSystemObject");
			}
			return xmlHttp;
		},
		
		//load file
		loadFileInMusicList:function(){
			//console.log(playList.length);
			var appendLi = $("#music_list .ulList");
			for(var i = 0 ; i < playList.length; i++){
				appendLi.append("<li id="+i+" class='music-select music-select-"+i+"'>"+playList[i].title+"</li>") 
			}
		},
		selectFile:function(){

		},

		//play selected music & load album pic
		playIndex:function(val){
			var self    = this;
			var len     = playList.length;
			var hasPlay = onPlay_btn.hasClass('icon-yulezanting');
			//console.log(hasPlay);
			if(val < len && val >= 0){
				if(hasPlay){
					var now = playList[val].url
					var pic = playList[val].img
					musicBox.find('source').attr('src',now);
					headImg.find('img').attr('src',pic);
					music.load();
					//console.log("我还有一口气在~~~~~~~~~~~~~~~~~~！！！");
					loadMusicSetIn = setInterval(self.loadMusic,1000);
				}
			}else{
				alert('没有歌曲了');
			}
		},
		// load music from service
		loadMusic:function(){
			var standBy;
			var i ;
			standBy = music.readyState;
			loadingArea.show();
			headImg.removeClass('rotateImg');
			if( standBy == 4){
				clearInterval(loadMusicSetIn);
				//console.log('为了部落！！！~~~~~~~~~~~~~~~~~~~~~~！！！' + standBy);
				loadingArea.hide();
				headImg.removeClass('rotateImg').addClass('rotateImg');
				music.play();
			}
		},

		//init play music
		initLoad:function(){
			//console.log('im here');
			var source = "<source src = '"+playList[0].url+"' type = 'audio/mpeg'></source>";
			var toppic = playList[0].img;
			musicBox.append(source);
			headImg.find('img').attr('src',toppic);
		}
	};
});
